﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;


public class SpriteFitScreen : MonoBehaviour {

    public enum FitType
    {
        EqualProportion,//依据宽高比与16：9 的比值进行等比例缩放
        NorEqualProportion,//不等比例缩放
    }
    public FitType m_FitType = FitType.EqualProportion;

    public static Vector2 StandardScreenSolution = new Vector2(1080,1920);
    public Vector2 originalScale = Vector2.one;

    //public bool UIScale = false;

    void Awake()
    {
        CalculateScale();

#if UNITY_EDITOR
        preWidth = Screen.width;
        preHeight = Screen.height ;
        preBaseOnWidth = baseOnWidth;
#endif
    }

#if UNITY_EDITOR

    int preWidth;
    int preHeight;
    float preBaseOnWidth = 0;
    private void Update()
    {
        if(Screen.width != preWidth || Screen.height != preHeight )
        {
            preWidth = Screen.width;
            preHeight = Screen.height;

            CalculateScale();
        }

        if(Time.frameCount % 30 == 0)
        {
            if ( preBaseOnWidth != this.baseOnWidth)
            {
                preBaseOnWidth = baseOnWidth;
                CalculateScale();
            }
        }
    }


#endif

    void Start()
    {

    }

    void CalculateScale()
    {
        switch (m_FitType)
        {
            case FitType.EqualProportion: OnScaleEqualProportion(); break;
            case FitType.NorEqualProportion: OnSacleWidthAndHeightSeparate(); break;
        }
    }

    /// <summary>
    /// 等比例缩放
    /// </summary>
    void OnScaleEqualProportion()
    {
        float sScale = GetEqualProportionScale();

     //   Debug.LogError("sScale:"+ sScale);

        OnScaleTransform(transform, new Vector2(sScale, sScale));

    }
    [Range(0.0f, 1)]
    public float baseOnWidth;
    public float GetEqualProportionScale()
    {
        var sScale = GetRatioScale();

        float baseOnWidthScale = 1;
             if (ClipOne)
                baseOnWidthScale = (sScale > 1) ? 1 : 1 / sScale;//大于1则取1
             else
                baseOnWidthScale = 1 / sScale;

        float baseOnHeightScale = 1;
            if (ClipOne)
                baseOnHeightScale = (sScale < 1) ? 1 :  sScale;//小于1则取1
            else
                baseOnHeightScale = sScale; ;
        
        sScale = baseOnWidth * baseOnWidthScale + (1 - baseOnWidth) * baseOnHeightScale;

        return sScale;
    }

    void OnScaleTransform(Transform trans, Vector2 scale)
    {
        //if (UIScale)
        //{
        //    var s = trans.GetComponent<RectTransform>().sizeDelta;
        //    trans.GetComponent<RectTransform>().sizeDelta = s *  scale;
        //}
        //else
        {
            trans.localScale = new Vector3(scale.x * originalScale.x, scale.y * originalScale.y, 1);
        }
    }
    

    /// <summary>
    ///长宽分别缩放
    /// <summary>
    void OnSacleWidthAndHeightSeparate()
    {
        Vector2 seperateScale = GetWidthAndHeightSeparateScale(this.baseOnWidth);// ,this.ClipOne);
        
        OnScaleTransform(transform, seperateScale);
    }
    public bool ClipOne = true;
    /// <summary>
    /// 
    /// </summary>
    /// <param name="baseOnWidth">是否缩放宽</param>
    /// <param name="ClipOne"></param>
    /// <returns></returns>
    public static Vector2 GetWidthAndHeightSeparateScale(float baseOnWidth)//,bool ClipOne)
    {
        var scale = GetRatioScale();

        float xNewScale = scale;
        float yNewScale = 1;

        //比原来宽
        if (scale > 1)
        {
            //屏幕比原来宽
            xNewScale = scale;
            
            yNewScale = 1;
        }
        else
        {
            //比原来窄
            yNewScale = 1 / scale;

            xNewScale = 1;
        }

        //if (this.baseOnWidth > 0)
        //{
        //    //高不变，宽变
        //    xNewScale /= yNewScale;
        //    yNewScale /= yNewScale;
        //}
        //else
        //{
        //    //宽不变，高变
        //    yNewScale /= xNewScale;
        //    xNewScale /= xNewScale;
        //}
        float v = baseOnWidth * yNewScale + (1 - baseOnWidth) * xNewScale;
        xNewScale /= v;
        yNewScale /= v;

      //  Debug.LogError("xNewScale:"+ xNewScale+ "  yNewScale:"+ yNewScale);

        return new Vector2(xNewScale, yNewScale);
    }

    /// <summary>
    /// 获得16：9与当前屏幕比的比值
    /// </summary>
    /// <returns></returns>
    public static float GetRatioScale()
    {
        float sScale = Rate / (StandardScreenSolution.x / StandardScreenSolution.y);
        //DYDebug.Log("GetRatioScale sScale:" + sScale);
        return sScale;
    }

    public static float Rate
    {
        get
        {
            if (Application.isEditor) return (float)Screen.width / (float)Screen.height;
            //手机支付后有可能屏幕长宽互换
            if (_screenWidth == 0)
                _screenWidth = Screen.width;

            if (_screenHeight == 0)
                _screenHeight = Screen.height;

            return _screenWidth / (float)_screenHeight;
        }
    }

    static int _screenWidth = 0;
    static int _screenHeight = 0;
}
